package hamaster.jpbc.gui;

import static javax.swing.BoxLayout.Y_AXIS;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class CommonConfigPanel extends JPanel implements ActionListener, ItemListener {
	private static final long serialVersionUID = -6307218735375891758L;

	private JButton ok;
	private JButton cancel;
	private ConfigPanel container;
	private boolean configChanged;

	private JComboBox hashCombo;

	public CommonConfigPanel(ConfigPanel container) {
		setLayout(new BoxLayout(this, Y_AXIS));
		this.container = container;
		configChanged = false;

		String[] hashes = { Messages.getString("CommonConfigPanel.0"), Messages.getString("CommonConfigPanel.1") }; //$NON-NLS-1$ //$NON-NLS-2$
		hashCombo = new JComboBox(hashes);
		hashCombo.addItemListener(this);
		JPanel hashPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		hashPanel.add(new JLabel(Messages.getString("CommonConfigPanel.2"))); //$NON-NLS-1$
		hashPanel.add(hashCombo);

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		ok = new JButton(Messages.getString("CommonConfigPanel.3")); //$NON-NLS-1$
		cancel = new JButton(Messages.getString("CommonConfigPanel.4")); //$NON-NLS-1$
		ok.addActionListener(this);
		cancel.addActionListener(this);
		buttonPanel.add(cancel);
		buttonPanel.add(ok);

		add(hashPanel);
		add(buttonPanel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.ok) {
			// TODO 保存设置
		}
		if (e.getSource() == this.cancel) {
			if (!configChanged) {
				container.setVisible(false);
				return;
			}
			int doCancel = JOptionPane.showConfirmDialog(container,
					Messages.getString("CommonConfigPanel.5"), Messages.getString("CommonConfigPanel.6"), //$NON-NLS-1$ //$NON-NLS-2$
					JOptionPane.YES_NO_OPTION,
					JOptionPane.WARNING_MESSAGE);
			if (doCancel == JOptionPane.YES_OPTION)
				container.setVisible(false);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			configChanged = true;
		}
	}
}
